//
//  RTCategoryController.m
//  RBToyAPI
//
//  Created by baxiang on 2018/2/1.
//  Copyright © 2018年 roobo. All rights reserved.
//

#import "RTCategoryController.h"
#import "PDMainMenuCell.h"
#import "PDMainMenuHeadCollectView.h"
#import "RTAlbumListController.h"
#import "RTAlbumInfoController.h"
@interface RTCategoryController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *categoryArray;
@end

@implementation RTCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCollectionView];
    [self fetchHomeCategory];
}

-(void)fetchHomeCategory{
    
    
/*
 babyAge获取逻辑
 NSInteger currAge = 0;
 NSString *ageStr = self.currentDeviceDetail.index_config;//currentDeviceDetail类型为 RBDevicesDetail
 if (ageStr&&[ageStr hasPrefix:@"app.homepage."]) {
 NSString *age = [ageStr substringWithRange:NSMakeRange(@"app.homepage.".length, 1)];
 currAge = [age integerValue];
 }
 */
    @weakify(self);
    [RBPlayerApi getCategoryList:0 block:^(RBCategoryList * list, NSError *error) {
      @strongify(self);
        [self categoryListScreen:list];
    }];
}
-(void)categoryListScreen:(RBCategoryList*)dataList
{
    NSArray<RBCategoryModel*> *categories = dataList.categories;
    if (categories.count!=0) {
        [self.categoryArray removeAllObjects];
    }
    for (RBCategoryModel *categoryModel in categories) {
        if ([categoryModel.attr isEqualToString:@"mod"]) {
            [self.categoryArray addObject:categoryModel];
        }
              //儿歌 故事 英语 国学 跳转逻辑
//        if ([categoryModel.attr isEqualToString:@"cls"]) {
//            RBCategoryModel*  recommedCate = [categoryModel copy];
//            NSArray* cateModels= recommedCate.albums;
//
//            //二级页面参数转化
//            RBAlbumModel *album =[cateModels objectAtIndex:0];
//            RBCategoryModel *cate  = [RBCategoryModel new];
//            cate.cID = album.aID;
//            cate.icon = album.img;
//            cate.title = album.title;
//            cate.attr = album.act;
//        }
    }
    [self.collectionView reloadData];
}
- (NSMutableArray *)categoryArray{
    if (!_categoryArray) {
        _categoryArray = [NSMutableArray new];
    }
    return _categoryArray;
}

- (void)setupCollectionView {
    CGFloat margin = 20;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = margin;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.alwaysBounceHorizontal = NO;
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    collectionView.bounces = NO;
    [collectionView setCanCancelContentTouches:YES];
    [collectionView registerClass:[PDMainMenuCell class] forCellWithReuseIdentifier:NSStringFromClass([PDMainMenuCell class])];
    [collectionView registerClass:[PDMainMenuHeadCollectView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PDMainMenuHeadCollectView class])];
    _collectionView = collectionView;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat padding = 23;
    CGFloat itemCount = 4;
    CGFloat margin = 20;
    CGFloat itemWidth = (self.view.width-padding*2-margin*(itemCount-1))/itemCount;
    return CGSizeMake(itemWidth, 115);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return  CGSizeMake(self.view.width, 47);
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader){
        PDMainMenuHeadCollectView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PDMainMenuHeadCollectView class]) forIndexPath:indexPath];
        headerView.categoryModel = [self.categoryArray objectAtIndex:indexPath.section];
        @weakify(self);
        headerView.moreContentBlock = ^(RBCategoryModel *categoryModel) {
            @strongify(self);
            RTAlbumListController *list = [RTAlbumListController new];
            list.categoryModel = categoryModel;
            [self.navigationController pushViewController:list animated:YES];
        };
        return headerView;
        }
    return nil;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.categoryArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    RBCategoryModel *categoryModel = [self.categoryArray objectAtIndex:section];
    return categoryModel.albums.count ;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PDMainMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PDMainMenuCell class]) forIndexPath:indexPath];
    RBCategoryModel *categoryModel = [self.categoryArray objectAtIndex:indexPath.section];
    cell.albumModel  = [categoryModel.albums objectAtIndex:indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RBCategoryModel *categoryModel = [self.categoryArray objectAtIndex:indexPath.section];
    RBAlbumModel *albumModel = [categoryModel.albums objectAtIndex:indexPath.row];
    if ([albumModel.act isEqualToString:@"cate"]) {
        RTAlbumListController *list = [RTAlbumListController new];
        list.albumModel = albumModel;
        [self.navigationController pushViewController:list animated:YES];
    }else {
        RTAlbumInfoController *albumInfoVC = [RTAlbumInfoController new];
        albumInfoVC.albumModel =  albumModel;
        [self.navigationController pushViewController:albumInfoVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
